﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cinema_Ticketing_System.Models;
using Cinema_Ticketing_System.Models.User;
using Cinema_Ticketing_System.Models.ViewModel;

namespace Cinema_Ticketing_System.Controllers
{
    public class UserController : Controller
    {

        int userid = 0;
        // GET: User
        public ActionResult Index()
        {
            var cid = -99;
            var fid = -88;
            Session["cid"] = cid;
            Session["fid"] = fid;
            return View();
            
        }
        /// <summary>
        /// 首次登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Login()
        {
            var cid = -99;
            var fid = -88;
            Session["cid"] = cid;
            Session["fid"] = fid;
            return View();
            
        }
        /// <summary>
        /// 输入密码登录
        /// </summary>
        /// <param name="id">账号</param>
        /// <param name="pwd">密码</param>
        /// <returns>是否成功登录</returns>
        [HttpPost]
        public ActionResult Login( string id, string pwd)
        {
            //优先判断是否输入了账号密码 如果未输入的话就跳转到用户的登陆界面
            if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(pwd))
                return View();
            //调用获得用户信息的方法 传递账号密码验证是否登陆成功
            Get_User_Information  user_Information = new Get_User_Information();
            var user = user_Information.GetUserInformation(id, pwd);
            //将信息存储到ViewBag里去 方便后续查询工作
            ViewBag.listuser = user;
            //如过未查询到则为空 显示登陆成功 否则登录失败
            string message = user != null ? "登录成功" : "登录失败";
            if(user != null)
            {
                Session["currentUser"] = user;
            }
            //返回到将数据返回到视图中去
            return Content(message, "text / plain");
        }

        /// <summary>
        /// 个人中心首次登陆
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Personal_Center()
        {
            userid = ((user_table)Session["currentUser"]).userid;
            Get_User_Indent get_User_Indent = new Get_User_Indent();
            ViewBag.listUserIndent = get_User_Indent.GetIndent(userid);

            User u1 = new User();
            ViewBag.listGetUSerIdentMessage= u1.GetUSerIdentMessage(get_User_Indent.GetIndent(userid));
            return View();
        }


        /// <summary>
        /// 后续修改密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Personal_Center(CreatUserViewModel model)
        {
            //如果有一项没有通过验证 则返回
            if (!ModelState.IsValid)
            {
                return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = false, message = "验证失败" }), "application/json");
            }
             
            //创建视图类转换未实体类
            user_table user = new user_table()
            {
                userid = ((user_table)Session["currentUser"]).userid,
                username=model.username,
                userpassword=model.newpwd,
                userphone=model.userphone,
            };
            
            User u1 = new User();

            //调用用户中的修改用户信息方法
            int rows = u1.ChangeUserMessage(user);
            //调用获得用户订单信息的方法

            string message = rows > 0 ? "修改成功" : "修改失败";
            return Content(message, "text / plain");
        }
        public ActionResult ChangePwd()
        {
            return View();
        }

        //首次登陆
        [HttpGet]
        public ActionResult Enroll()
        {
            return View();
        }
        //点击提交
        [HttpPost]
        public ActionResult Enroll( AddUserViewModel userViewModel)
        {
            //如果有一项没有通过验证 则返回
            if (!ModelState.IsValid)
            {
                return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = false, message = "验证失败" }), "application/json");
            }

            //创建视图类转换未实体类
            user_table user = new user_table()
            {
                username = userViewModel.username,
                userpassword = userViewModel.newpwd,
                userphone = userViewModel.userphone,
            };

            User u1 = new User();

            //调用用户中的修改用户信息方法
            int rows = u1.AddUser(user);
            //调用获得用户订单信息的方法

            string message = rows > 0 ? "注册成功" : "注册失败";
            return Content(message, "text / plain");
        }
    }
}